package com.ccgk.logistics.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ccgk.common.MyCollectionUtils;
import com.ccgk.common.page.PaginationVo;
import com.ccgk.common.web.RequestUtils;
import com.ccgk.logistics.domain.Container;
import com.ccgk.logistics.domain.Country;
import com.ccgk.logistics.domain.Currency;
import com.ccgk.logistics.service.ContainerService;
import com.ccgk.logistics.service.CurrencyService;

@Controller
@RequestMapping("/currency")
public class CurrencyController {
	@Autowired
	private CurrencyService currencyService;
	
	/**查询列表*/
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		int currentPage = RequestUtils.getInt(request,"currentPage",1);
		int pageSize = RequestUtils.getInt(request,"pageSize",PaginationVo.DEFAULT_PAGESIZE);
		
		CurrencyService.CurrencyCriteria  criteria = new CurrencyService.CurrencyCriteria();
		String name = request.getParameter("name");
		if(StringUtils.isNotBlank(name))
			criteria.nameLike(name);
		PaginationVo<Currency> result = currencyService.findList(criteria, currentPage, pageSize);
		
		request.setAttribute("result",result);
		return "currency/list";
	}
	
	/**获取单个*/
	@RequestMapping("/{id}")
	public String getById(@PathVariable("id")Integer id,Model model){
		Currency currency = currencyService.getById(id);
		model.addAttribute("obj", currency);
		return "currency/edit";
	}
	
	/**更新信息*/
	@ResponseBody
	@RequestMapping(value="/{id}",method=RequestMethod.POST)
	public String update(Currency currency){
		Currency _currency  = currencyService.getByName(currency.getName());
		if(_currency != null && !_currency.getId().equals(currency.getId()))
			return "{flag:1}";
		currencyService.update(currency);
		return "{flag:0}";
	}
	/**新增页*/
	@RequestMapping("/add")
	public String showAdd(){
		return "currency/add";
	}
	/**新增*/
	@ResponseBody
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public String add(Currency currency,HttpServletResponse response){
		if(currencyService.getByName(currency.getName()) != null){
			return "{flag:1}";
		}
		currencyService.add(currency);
		return "{flag:0}";
	}
	/**删除*/
	@ResponseBody
	@RequestMapping("/delete")
	public String delete(@RequestParam("id")String ids,Model model){
		Integer[] idIntArray = MyCollectionUtils.splitToIntArray(ids);
		currencyService.batchDelete(idIntArray);
		return "{success:true}";
	}
}
